package com.asset.dispoal.verdict.service.common;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.verdict.domain.db.entity.GbJudgmentBookItem;
import com.asset.dispoal.verdict.domain.db.mapper.GbJudgmentBookItemMapper;
import com.asset.dispoal.verdict.dto.request.JudgmentItemRequest;
import com.asset.dispoal.verdict.dto.response.JudgmentItemResponse;
import com.asset.dispoal.verdict.service.converter.GbJudgmentBookItemConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @description:
 * @author: yf
 * @date: 2022-09-06
 */
@Repository
public class QueryJudgmentItemCommand implements BaseCommand<JudgmentItemRequest, Result<PageVO<JudgmentItemResponse>>> {
    @Autowired
    private GbJudgmentBookItemMapper gbJudgmentBookItemMapper;

    @Override
    public Result<PageVO<JudgmentItemResponse>> execute(JudgmentItemRequest request) {
        GbJudgmentBookItem judgmentBookItem = GbJudgmentBookItemConverter.INSTANCE.reqToDo(request);
        Integer count = gbJudgmentBookItemMapper.count(judgmentBookItem);
        Page<GbJudgmentBookItem> page = PagingQuery.getPage(count, judgmentBookItem);
        PageVO<JudgmentItemResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<GbJudgmentBookItem> judgmentBookItems = gbJudgmentBookItemMapper.queryAllByLimit(page);
            List<JudgmentItemResponse> bsCaseResponses = GbJudgmentBookItemConverter.INSTANCE.dosToRes(judgmentBookItems);
            pageVO.setResult(bsCaseResponses);
        }
        return Result.ok(pageVO);
    }
}
